package cbfg.http.util

import android.util.Log

internal object Logger {
    private const val MAX_LENGTH = 1350

    private var globalTag: String = "HttpManager"
    private var isShowLog = false

    fun init(isShowLog: Boolean, tag: String? = null) {
        this.isShowLog = isShowLog
        if (tag != null) {
            this.globalTag = tag
        }
    }

    /*
     * log输出
     * */
    fun i(objects: Any?) {
        if (!isShowLog) {
            return
        }
        val tag = globalTag
        val msg = objects?.toString() ?: "null"

        /**
         * 检查长度，如果超出MAX_LENGTH则分段输出
         */
        val countOfSub = msg.length / MAX_LENGTH
        if (countOfSub == 0) {
            //msg.length<MAX_LENGTH
            Log.i(tag, msg)
            return
        }
        //msg.length>MAX_LENGTH
        var index = 0
        for (i in 0 until countOfSub) {
            val sub = msg.substring(index, index + MAX_LENGTH)
            Log.i(tag, sub)
            index += MAX_LENGTH
        }
        Log.i(tag, msg.substring(index, msg.length))
    }
}